﻿using B.S.BaseData.Api.Write.Applicantion.Command.GoodsCommand;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interface;
using B.S.BataBase.Domain.Entitys;
using MediatR;

namespace B.S.BaseData.Api.Write.Applicantion.CommandHandler.GoodsCommandHandler
{
    public class GoodsInfobatchDeleteCommandHandler : IRequestHandler<GoodsInfobatchDeleteCommand, APIResult<int>>
    {
        private readonly IBaseRepository<GoodsInfo> _goodsbaseRepository;
        public GoodsInfobatchDeleteCommandHandler(IBaseRepository<GoodsInfo> goodsbaseRepository)
        {
            _goodsbaseRepository = goodsbaseRepository;
        }
        public async Task<APIResult<int>> Handle(GoodsInfobatchDeleteCommand request, CancellationToken cancellationToken)
        {
            int c = 0;
            APIResult<int> result = new APIResult<int>();
            foreach (var item in request.Ids)
            {
                var list = await _goodsbaseRepository.GetValues(x => x.Id == item);
                list.IsDeleted = true;
                c+= await _goodsbaseRepository.UpdateAsync(list);
            }
            
            result.Code = ApiEnum.Success;
            result.Msg = "商品信息批量删除成功";
            result.Data = c;
            return await Task.FromResult(result);
        }
    }
}
